<?php
	/*
	Info: Sends Friends request
	Author: Pritam Pebam
	Version: 1.0
	Contact: ptamzz@gmail.com; @ptamzz
	URL: http://12bubbles.com
	*/
	
	session_start();
	require_once('../inc/fnc.php');
/* 	require_once('../inc/conn.php');
	require_once('../inc/settings.php');
	require_once('../inc/constants.php'); */
	
	// Initializing variables
	$frenId = $_POST['frenId'];
	$userId = $_SESSION['userId'];
	
	// Checking this request is already send or not
	$query = "SELECT * from followme WHERE followerid = {$userId} and followingid = {$frenId} and delflag = 'N'";
	$result = mysql_query($query);
	if(mysql_num_rows($result) == 0) {
		//Establishing fren connection
		$queryinsert = "INSERT INTO followme (followerid,followingid,delflag,crtdtime) VALUES ({$userId},{$frenId},'N',NOW())";
		$resultinsert = mysql_query($queryinsert);
	
		//Sending Email Notification
		if($resultinsert) {
			$username = getUsername($userId);
			$email = getEmail($frenId);
			
			// Sending Activation link to user
			$subject = $username.' has started following you'; // subject
			
			$message = $emailTop;
			$message .= "	<p>{$username} has started following you</p>
							<br/>
							Check out <a href='".DOMAIN."/index.php?page=" . $username . "'>{$username}'s</a> profile.";
			$message .= $emailBottom;

			// Mail it
			$error['type'] = (sendMail($email, $subject, $message)) ? 'false' : 'true';

			echo json_encode($error);
		} else {
			$error['type'] = 'true';
			echo json_encode($error);
		} // if($resultinsert) {
	} else {
		$error['type'] = 'sent';
		echo json_encode($error);
	}
	
?>